package com.ruoyi.crm.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.crm.domain.bo.CrmProspectInfoBo;
import com.ruoyi.crm.domain.vo.CrmProspectInfoVo;
import com.ruoyi.crm.domain.CrmProspectInfo;
import com.ruoyi.crm.mapper.CrmProspectInfoMapper;
import com.ruoyi.crm.service.ICrmProspectInfoService;

import java.util.List;
import java.util.Collection;

/**
 * 商机管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-10
 */
@RequiredArgsConstructor
@Service
public class CrmProspectInfoServiceImpl implements ICrmProspectInfoService {

    private final CrmProspectInfoMapper baseMapper;

    /**
     * 查询商机管理
     */
    @Override
    public CrmProspectInfoVo queryById(Long prospectId){
        return baseMapper.selectVoById(prospectId);
    }

    /**
     * 查询商机管理列表
     */
    @Override
    public TableDataInfo<CrmProspectInfoVo> selectProspectPageList(CrmProspectInfoBo bo, PageQuery pageQuery) {

        LambdaQueryWrapper<CrmProspectInfo> queryWrapper = new LambdaQueryWrapper<>();
        //模糊查询商机名称
        if(StringUtils.isNotBlank(bo.getProspectName())){
            queryWrapper.like(CrmProspectInfo::getProspectName, bo.getProspectName());
        }
        //查询商机状态
        if(StringUtils.isNotBlank(bo.getOpportunityStatus())){
            queryWrapper.eq(CrmProspectInfo::getOpportunityStatus, bo.getOpportunityStatus());

        }

        Page<CrmProspectInfoVo> page = baseMapper.selectVoPage(pageQuery.build(), queryWrapper);
        return TableDataInfo.build(page);
    }

    /**
     * 查询商机管理列表
     */
    @Override
    public List<CrmProspectInfoVo> queryList(CrmProspectInfoBo bo) {
        LambdaQueryWrapper<CrmProspectInfo> queryWrapper = new LambdaQueryWrapper<>();
        //模糊查询商机名称
        if(StringUtils.isNotBlank(bo.getProspectName())){
            queryWrapper.like(CrmProspectInfo::getProspectName, bo.getProspectName());
        }
        //查询商机状态
        if(StringUtils.isNotBlank(bo.getOpportunityStatus())){
            queryWrapper.eq(CrmProspectInfo::getOpportunityStatus, bo.getOpportunityStatus());

        }
        return baseMapper.selectVoList(queryWrapper);
    }

//    private LambdaQueryWrapper<CrmProspectInfo> buildQueryWrapper(CrmProspectInfoBo bo) {
//        Map<String, Object> params = bo.getParams();
//        LambdaQueryWrapper<CrmProspectInfo> lqw = Wrappers.lambdaQuery();
//        lqw.eq(bo.getStageId() != null, CrmProspectInfo::getStageId, bo.getStageId());
//        lqw.eq(bo.getCustomerId() != null, CrmProspectInfo::getCustomerId, bo.getCustomerId());
//        lqw.eq(bo.getUserId() != null, CrmProspectInfo::getUserId, bo.getUserId());
//        lqw.eq(bo.getProspectProductId() != null, CrmProspectInfo::getProspectProductId, bo.getProspectProductId());
//        lqw.eq(bo.getProjectApproveId() != null, CrmProspectInfo::getProjectApproveId, bo.getProjectApproveId());
//        lqw.eq(bo.getContactId() != null, CrmProspectInfo::getContactId, bo.getContactId());
//        lqw.eq(StringUtils.isNotBlank(bo.getProductRequirement()), CrmProspectInfo::getProductRequirement, bo.getProductRequirement());
//        lqw.eq(StringUtils.isNotBlank(bo.getReviewStatus()), CrmProspectInfo::getReviewStatus, bo.getReviewStatus());
//        lqw.like(StringUtils.isNotBlank(bo.getProspectName()), CrmProspectInfo::getProspectName, bo.getProspectName());
//        lqw.eq(StringUtils.isNotBlank(bo.getProductDetails()), CrmProspectInfo::getProductDetails, bo.getProductDetails());
//        lqw.eq(bo.getPrice() != null, CrmProspectInfo::getPrice, bo.getPrice());
//        lqw.eq(bo.getRecruitPrice() != null, CrmProspectInfo::getRecruitPrice, bo.getRecruitPrice());
//        lqw.eq(StringUtils.isNotBlank(bo.getOpportunityStatus()), CrmProspectInfo::getOpportunityStatus, bo.getOpportunityStatus());
//        lqw.eq(StringUtils.isNotBlank(bo.getChangeStatus()), CrmProspectInfo::getChangeStatus, bo.getChangeStatus());
//        lqw.eq(StringUtils.isNotBlank(bo.getSmallNode()), CrmProspectInfo::getSmallNode, bo.getSmallNode());
//        lqw.eq(StringUtils.isNotBlank(bo.getNodeContent()), CrmProspectInfo::getNodeContent, bo.getNodeContent());
//        lqw.eq(bo.getNextTime() != null, CrmProspectInfo::getNextTime, bo.getNextTime());
//        lqw.eq(bo.getEstimatedTime() != null, CrmProspectInfo::getEstimatedTime, bo.getEstimatedTime());
//        lqw.eq(bo.getOverTime() != null, CrmProspectInfo::getOverTime, bo.getOverTime());
//        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), CrmProspectInfo::getUrl, bo.getUrl());
//        lqw.like(StringUtils.isNotBlank(bo.getOriginalName()), CrmProspectInfo::getOriginalName, bo.getOriginalName());
//        return lqw;
//    }

    /**
     * 新增商机管理
     */
    @Override
    public Boolean insertByBo(CrmProspectInfoBo bo) {
        CrmProspectInfo add = BeanUtil.toBean(bo, CrmProspectInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProspectId(add.getProspectId());
        }
        return flag;
    }

    /**
     * 修改商机管理
     */
    @Override
    public Boolean updateByBo(CrmProspectInfoBo bo) {
        CrmProspectInfo update = BeanUtil.toBean(bo, CrmProspectInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CrmProspectInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商机管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
